<template>
  <div class='top-category'>
    <div class="container">
      <XtxBread separator="/">
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem>{{ cateName }}</XtxBreadItem>
      </XtxBread>
    <!-- 轮播图 -->
    <XtxSlider :data="slidersData" />
    <!-- 全部分类 -->
    <div class="sub-list">
      <h3>全部分类</h3>
      <ul>
        <li v-for="category in categoryList" :key="category.id">
            <a href="javascript:;">
            <img :src="category.picture" >
            <p>{{category.name}}</p>
            </a>
        </li>
     </ul>
</div>
  <!-- 分类关联商品 -->
  <div class="ref-goods" v-for="category in categoryList" :key="category.id">
      <div class="head">
        <h3>-  {{category.name}}  -</h3>
        <XtxMore to="/" />
      </div>
      <div class="body">
        <goodsItem :good="good" v-for="good in category.goods" :key="good.id"/>
        <!-- <RouterLink to="/" class="goods-item" > -->
          <!-- <img
            :src="good.picture"
            alt=""
          />
          <p class="name ellipsis">{{good.name}}</p>
          <p class="desc ellipsis">{{good.desc}}</p>
          <p class="price">&yen;{{good.price}}</p> -->
        <!-- </RouterLink> -->
      </div>
  </div>
 </div>
</div>
</template>

<script>
import { useBanner } from '@/hooks/page'
import { findAllCategory } from '@/api/category'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import goodsItem from './components/goods-item.vue'

// 封装成函数
const useCategory = () => {
  // vue3新方法 useRoute()===this.$route
  const route = useRoute()
  //   定义变量
  const categoryList = ref([])
  // 分类名
  const cateName = ref('')
  //   调用接口 接收数据
  const getCategroyList = async () => {
    //   传入id 解构
    const { result } = await findAllCategory(route.params.id)
    console.log('111', result)
    // 定义的是数组 接收的是对象 所以要多点一级children
    categoryList.value = result.children
    cateName.value = result.name
  }
  //   挂载钩子函数
  onMounted(() => {
    getCategroyList()
  })
  //   返回出去
  return { categoryList, cateName }
}
export default {
  name: 'TopCategory',
  components: {
    goodsItem
  },
  setup () {
    const { slidersData } = useBanner()
    // 调用方法 解构需要的数据
    const { categoryList, cateName } = useCategory()
    // 页面需要使用 所以要return出去
    return { slidersData, categoryList, cateName }
  }
}
</script>

<style scoped lang="less">

.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
</style>
